import typing
import jinja2
from src.generate_img import generate_img, return_bytes


def generate_from_file(path_file):
    with open(path_file, mode='r') as file:
        codes = file.readlines()

    for code in codes:
        image_code = generate_img(code)
        image_code.save("barcode.png")
        return_bytes(image_code, True)


def gen_templete(codes: typing.Iterable, template_path: str, save_result_path: str = "result.html"):
    with open(template_path) as file_:
        template = jinja2.Template(file_.read())
    html = template.render(codes=(
        {
            "img": return_bytes(generate_img(code), True),
            "code": code
        } for code in codes
    ))
    with open(save_result_path, 'w') as file:
        file.write(html)
